package com.demo.service.impl;

import com.demo.dao.ProductDao;
import com.demo.model.Product;
import com.demo.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ProductServiceImpl implements ProductService {
    @Autowired
    private ProductDao productDao;

    @Override
    public int save(Product product) {
        return productDao.insert(product);
    }

    @Override
    public Product getProductById(Integer id) {
        return productDao.selectOne(id);
    }

    @Override
    public Integer updateProduct(Product product) {
        if (product.getId() != null && product.getId() > 0) {
            return productDao.update(product);
        }
        return productDao.insert(product);
    }

    @Override
    public Integer deleteProduct(int id) {
        return productDao.delete(id);
    }

    @Override
    public List<Product> getProducts(String name) {
        return productDao.select(name);
    }
}
